﻿using Mobileterminal.Common;
using Mobileterminal.Dtos;
using Newtonsoft.Json;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Mobileterminal.Forms.Zyy
{
    public partial class GetReturn : UIPage
    {
        public GetReturn()
        {
            InitializeComponent();
            Load();
            Loads();
        }

        private void GetReturn_Load(object sender, EventArgs e)
        {

        }

       
        private async void uiDataGridView3_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
          
        }
        private async Task Load()
        {

            var url = "https://localhost:7224/api/Returns/GetReturn";
            var json = await HttpClientHelper.ClientAsync("GET", url);
            var result = Newtonsoft.Json.JsonConvert.DeserializeObject<Result<List<Returns>>>(json);
            if (result?.Code == 200)
            {
                foreach (var item in result.Data)
                {
                    item.StatusName = item.Status == 0 ? "提交" : "接受";
                }
                uiDataGridView3.DataSource = result.Data;

            }





        }
        private async Task Loads()
        {

            var url = "https://localhost:7224/api/Returns/GetReturn";
            var json = await HttpClientHelper.ClientAsync("GET", url);
            var result = Newtonsoft.Json.JsonConvert.DeserializeObject<Result<List<Returns>>>(json);
            if (result?.Code == 200)
            {

                uiDataGridView1.DataSource = result.Data;

            }





        }


        #region 操作 修改
        /// <summary>
        /// 表格的重绘事件 DataGridViewCellPaintingEventArgs
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void uiDataGridView3_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)//重绘“操作列”,使其有三个按钮
        {
            //var a=e as DataGridViewCellPaintingEventArgs;

            if (e.ColumnIndex >= 0 && e.RowIndex >= 0)
            {
                //
                if (this.uiDataGridView1.Columns[e.ColumnIndex].HeaderText == "操作" && this.uiDataGridView1.Rows[e.RowIndex].Cells["ApplicationId"].Value != null)
                {
                    StringFormat sf = StringFormat.GenericDefault.Clone() as StringFormat;//设置重绘入单元格的字体样式
                    sf.FormatFlags = StringFormatFlags.DisplayFormatControl;
                    sf.Alignment = StringAlignment.Center;
                    sf.LineAlignment = StringAlignment.Center;
                    sf.Trimming = StringTrimming.EllipsisCharacter;

                    e.PaintBackground(e.CellBounds, false);//重绘边框

                    //设置要写入字体的大小
                    System.Drawing.Font myFont = new System.Drawing.Font("微软雅黑", 10F, System.Drawing.FontStyle.Underline, System.Drawing.GraphicsUnit.Point, ((byte)(134)));

                    SizeF sizeMod = e.Graphics.MeasureString("编辑", myFont);
                    SizeF sizeDel = e.Graphics.MeasureString("删除", myFont);

                    float fMod = sizeMod.Width / (sizeDel.Width + sizeMod.Width);
                    float fDel = sizeDel.Width / (sizeDel.Width + sizeMod.Width);


                    //设置每个“按钮的边界”
                    RectangleF rectMod = new RectangleF(e.CellBounds.Left, e.CellBounds.Top, e.CellBounds.Width * fMod, e.CellBounds.Height);
                    //RectangleF rectMod = new RectangleF(rectPrint.Right, e.CellBounds.Top, e.CellBounds.Width * fMod, e.CellBounds.Height);
                    RectangleF rectDel = new RectangleF(rectMod.Right, e.CellBounds.Top, e.CellBounds.Width * fDel, e.CellBounds.Height);

                    Graphics grfx = CreateGraphics();
                    Color colorUse = Color.FromArgb(51, 136, 255);
                    Color colorPrint = Color.FromArgb(51, 136, 255);

                    e.Graphics.DrawString("编辑", myFont, new SolidBrush(colorUse), rectMod, sf);
                    e.Graphics.DrawString("删除", myFont, new SolidBrush(colorPrint), rectDel, sf); //绘制“按钮”
                    e.Handled = true;
                }
            }
        }
        /// <summary>
        /// 单元格点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private async void uiDataGridView3_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            try
            {
                if (e.ColumnIndex >= 0 && e.RowIndex >= 0)
                {
                    Point curPosition = e.Location;//当前鼠标在当前单元格中的坐标
                    if (this.uiDataGridView3.Columns[e.ColumnIndex].HeaderText == "操作" && uiDataGridView3.Rows[e.RowIndex].Cells["ReturnApplicationId"].Value != null)
                    {
                        Graphics g = this.uiDataGridView3.CreateGraphics();
                        System.Drawing.Font myFont = new System.Drawing.Font("微软雅黑", 10F, System.Drawing.FontStyle.Underline, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
                        //bool isPrintshow = (bool)(this.uiDataGridView1.Rows[e.RowIndex].Cells["isPrintshow"].Value);
                        //var isUse = (bool)this.uiDataGridView1.Rows[e.RowIndex].Cells["isUse"].Value;
                        //var isUseStr = !isUse && isPrintshow ? "已生单" : "生单";

                        //SizeF sizeprint = g.MeasureString(isUseStr, myFont);
                        SizeF sizeMod = g.MeasureString("编辑", myFont);
                        SizeF sizeDel = g.MeasureString("撤销", myFont);

                        Rectangle rectTotal = new Rectangle(0, 0, this.uiDataGridView3.Columns[e.ColumnIndex].Width, this.uiDataGridView3.Rows[e.RowIndex].Height);
                        RectangleF rectDel;
                        //RectangleF rectMod;
                        RectangleF rectMod;

                        float fDel = sizeDel.Width / (sizeDel.Width + sizeMod.Width);
                        //float fMod = sizeMod.Width / (sizeDel.Width + sizeMod.Width + sizeprint.Width);
                        float fMod = sizeMod.Width / (sizeDel.Width + sizeMod.Width);

                        rectMod = new RectangleF(rectTotal.Left, rectTotal.Top, rectTotal.Width * fMod, rectTotal.Height);
                        //rectMod = new RectangleF(rectPrint.Right, rectTotal.Top, rectTotal.Width * fMod, rectTotal.Height);
                        rectDel = new RectangleF(rectMod.Right, rectTotal.Top, rectTotal.Width * fDel, rectTotal.Height);
                        //rectMod = new RectangleF(0, 0, 0, 0);

                        //判断当前鼠标在哪个“按钮”范围内
                        if (rectMod.Contains(curPosition))//编辑
                        {
                            /*  upshelf addshelf = new upshelf(ShelfId);
                              addshelf.Show();*/
                            //打开新的form窗体
                            //  var NumericalOrderDetail = this.uiDataGridView1.Rows[e.RowIndex].Cells["ShelfId"].Value.ToString();
                            var NumericalOrderDetail = this.uiDataGridView3.Rows[e.RowIndex].Cells["ReturnApplicationId"].Value.ToString();
                            UpReaction addshelf = new UpReaction(NumericalOrderDetail);
                            addshelf.Show();
                            //打开新的form窗体  
                        }
                       
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 在按钮上鼠标箭头变成小手样式
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void uiDataGridView3_CellMouseMove(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.ColumnIndex >= 0 && e.RowIndex >= 0)
            {
                Point curPosition = e.Location;//当前鼠标在当前单元格中的坐标
                if (this.uiDataGridView3.Columns[e.ColumnIndex].Name == "操作")
                {
                    this.Cursor = Cursors.Hand;
                    //解决绘图时画面闪烁
                    SetStyle(ControlStyles.UserPaint, true);
                    SetStyle(ControlStyles.AllPaintingInWmPaint, true); // 禁止擦除背景.
                    SetStyle(ControlStyles.DoubleBuffer, true); // 双缓冲

                }
            }
        }

        #endregion
        private void uiDataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void GetReturn_Initialize(object sender, EventArgs e)
        {

        }
    }
}
